var assert = require('../assert');
exports.detail = function(req,res,next){
    var JADE_VAR = assert.getJADEVAR();
    var productId =req.params.id;
    var userId = assert.getUserId(req);
    var isVisited = req.cookies.isVisited;
    var url = '/kdProduct/' + productId;
    if (userId) {
        url = url + '?userId=' + userId;
    }
    if(!isVisited){
        if(userId){
            url += '&unvisited=1'
        }else{
            url += '?unvisited=1'
        }
    }
    if(productId){
        assert.apiParseRequest('get',url,{}).then(function(results){
            if(results){
                JADE_VAR.productInfo = results;
                res.cookie('isVisited','1',{path:'/'});
                res.render('product/product',JADE_VAR);
            }else{
                res.render('error',assert.getRes(102,"服务失败",{proGet:true},true));
            }
        },function(err){
            res.render('error',assert.getRes(102,"服务失败",{},true));
            //错误处理
        })
    }else{
      res.render('error',assert.getRes(101,"获取商品ID失败",{},true));
    }
};
/**
 * 搜索表示该店铺内商品的搜索
 * @param req
 * @param res
 * @param next
 */
exports.search = function (req, res, next) {
    var searchKey = req.query.keyword;
    var shopId = req.params.shopId;
    var JADE_VAR = assert.getJADEVAR();
    JADE_VAR.shopId = shopId;
    JADE_VAR.searchKey = '';
    JADE_VAR.listProduct = [];
    if (!searchKey) {
        //表示默认显示页面
        res.render('product/search', JADE_VAR);
    } else {
        JADE_VAR.searchKey = searchKey;
        //JADE_VAR.listProduct = [
        //    {
        //        'objectId':'AsSC6otYcG',
        //        'image':"http://7xlaxq.com1.z0.glb.clouddn.com/Fskvsy-FWPZC1pRUytaVuxMO9hrA",
        //        'sallPrice':68.8,
        //        'title':"我家的大金毛"
        //    }
        //];

        assert.apiParseRequest('GET', '/kdGoodsSearch?storeId=' + shopId + '&keyWord=' + encodeURI(searchKey)+'&page=1').then(function (searchResults) {
            if(searchResults){
                JADE_VAR.listProduct = searchResults;
            }else{
                JADE_VAR.listProduct = [];
            }
            res.render('product/search', JADE_VAR);
        }, function (err) {
            console.log(err);
        } )
    }
};